package com.dweb.jiugongzi.service;

import com.dweb.jiugongzi.data.TableValuesJsonToClass;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.rpc.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;

import java.util.HashMap;
import java.util.Map;

public class JsToJava extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    private JavaRemote remote = new JavaRemote();
    // FA在请求PA服务时会调用AbilityconnectAbility连接PA，连接成功后，需要在onConnect返回一个remote对象，供FA向PA发送消息

    @Override
    protected IRemoteObject onConnect(Intent intent) {
        super.onConnect(intent);
        return remote.asObject();
    }

    class JavaRemote extends RemoteObject implements IRemoteBroker {

        JavaRemote() {
            super("JsApi_JavaRemote");
        }
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option){
            //开始处理数据
            String zsonStr = data.readString();
            TableValuesJsonToClass tablevalues = ZSONObject.stringToClass(zsonStr, TableValuesJsonToClass.class);
            System.out.println("-----"+tablevalues.getTable()+"///"+tablevalues.getValues());


            switch (code){
                case 1001:{
                    //开始处理数据
                    Intent secondIntent = new Intent();
                    Operation operation = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName("com.dweb.jiugongzi")
                            .withAbilityName("com.dweb.jiugongzi.Image")
                            .build();
                    secondIntent.setOperation(operation);
                    // 通过AbilitySlice的startAbility接口实现启动另一个页面
                    startAbility(secondIntent);
                    //通信成功信息发送前端
                    Map<String, Object> zsonResult = new HashMap<String, Object>();
                    zsonResult.put("code", 0);
                    zsonResult.put("msg", "跳转通信成功 -- 1001");
                    System.out.println("=====跳转通信成功 -- 1001");
                    reply.writeString(ZSONObject.toZSONString(zsonResult));
                    break;
                }
                default:{
                    reply.writeString("java返回通信失败 -- no code");
                    return  false;
                }
            }
            return true;
        }

        @Override
        public IRemoteObject asObject() {
            return this;
        }



    }



    @Override
    public void onStart(Intent intent) {
        HiLog.error(LABEL_LOG, "JsToJava::onStart");
        super.onStart(intent);
    }

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.info(LABEL_LOG, "JsToJava::onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        HiLog.info(LABEL_LOG, "JsToJava::onStop");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
    }

    @Override
    public void onDisconnect(Intent intent) {
    }
}